package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.InventoryProductDao;
import com.xbongbong.saas.domain.entity.BusinessProductEntity;
import com.xbongbong.saas.domain.entity.InventoryProductEntity;
import com.xbongbong.saas.model.InventoryProductModel;
import com.xbongbong.saas.util.StandardizeDataUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author feng.zheng
 * @version v1.0
 * @date 2019/2/28 15:51
 * @since v1.0
 */
@Service("inventoryProductModel")
public class InventoryProductModelImpl implements InventoryProductModel {
    @Resource
    private InventoryProductDao inventoryProductDao;
    /**
     * 插入数据
     *
     * @param entity 产品实体数据
     * @return 是否成功
     * @author feng.zheng
     * @date 2019/2/21 15:18
     **/
    @Override
    public Integer insert(InventoryProductEntity entity) {
        long now = DateTimeUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        StandardizeDataUtil.standardizeNotString(entity);
        return inventoryProductDao.insert(entity);
    }

    /**
     * 更新数据
     *
     * @param entity 产品实体数据
     * @return 是否成功
     * @author feng.zheng
     * @date 2019/2/21 15:18
     **/
    @Override
    public Integer update(InventoryProductEntity entity) {
        long now = DateTimeUtil.getInt();
        entity.setUpdateTime(now);
        StandardizeDataUtil.standardizeNotString(entity);
        return inventoryProductDao.update(entity);
    }

    /**
     * 根据主键删除记录
     *
     * @param key    主键
     * @param corpid 公司id
     * @return java.lang.Integer
     * @author feng.zheng
     * @date 2019/2/21 15:18
     */
    @Override
    public Integer deleteByKey(Long key, String corpid) {
        return inventoryProductDao.deleteByKey(key, corpid);
    }

    /**
     * 删除多条盘点单下的所有产品
     *
     * @param corpid       公司ID
     * @param inventoryIdIn 盘点单Id集合
     * @return java.lang.Integer
     * @author feng.zheng
     * @date 2019/2/21 15:18
     */
    @Override
    public Integer deleteByInventoryIdIn(String corpid, List<Long> inventoryIdIn) {
        return inventoryProductDao.deleteByInventoryIdIn(corpid, inventoryIdIn);
    }

    /**
     * 删除入库单下的所有产品
     *
     * @param refId  id集合
     * @param corpid 公司id
     * @return java.lang.Integer
     * @author feng.zheng
     * @date 2019/2/21 15:18
     */
    @Override
    public Integer deleteByInventoryId(Long refId, String corpid) {
        return inventoryProductDao.deleteByInventoryId(refId, corpid);
    }

    /**
     * 获取一条入库单产品数据
     *
     * @param key    采购合同产品ID
     * @param corpid 公司id
     * @return InventoryProductEntity 一条数据实体
     * @author feng.zheng
     * @date 2019/2/21 15:18
     */
    @Override
    public InventoryProductEntity getByKey(Long key, String corpid) {
        return inventoryProductDao.getByKey(key, corpid);
    }

    /**
     * 多参数查询多条数据
     *
     * @param param 参数集合
     * @return List<OutstockProductEntity> 数据集合
     * @author feng.zheng
     * @date 2019/2/21 15:18
     */
    @Override
    public List<InventoryProductEntity> findEntitys(Map<String, Object> param) {
        return inventoryProductDao.findEntitys(param);
    }

    /**
     * 模糊查询
     *
     * @param corpid       参数集合
     * @param nameOrNoLike 模糊查询值
     * @return List<Long> 数据集合
     * @author feng.zheng
     * @date 2019/2/21 15:18
     */
    @Override
    public List<Long> findInventoryIdsByRelaProNameOrNo(String corpid, String nameOrNoLike) {
        return inventoryProductDao.findInventoryIdsByRelaPNameOrNo(corpid, nameOrNoLike);
    }

    /**
     * 获取盘点单关联产品
     *
     * @param id     id
     * @param corpid 公司id
     * @return 产品
     */
    @Override
    public List<InventoryProductEntity> getProductsByInventoryId(Long id, String corpid) {
        // 获取关联产品
        Map<String, Object> modelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        modelMap.put(ParameterConstant.CORPID, corpid);
        modelMap.put("inventoryId", id);
        modelMap.put(ParameterConstant.DEL, DelEnum.NORMAL.getDel());
        modelMap.put(ParameterConstant.ORDER_BY_STR, StringConstant.SAAS_LINK_BUSINESS_ID);
        List<InventoryProductEntity> inventoryProductList = findEntitys(modelMap);
        return inventoryProductList;
    }

    @Override
    public List<InventoryProductEntity> getProductsByInventoryId(Long id, String corpid, List<Integer> delIn) {
        // 获取关联产品
        Map<String, Object> modelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        modelMap.put(ParameterConstant.CORPID, corpid);
        modelMap.put("inventoryId", id);
        modelMap.put("delIn", delIn);
        modelMap.put(ParameterConstant.ORDER_BY_STR, BasicConstant.SORT);
        return findEntitys(modelMap);
    }

    /**
     * 获取记录数
     *
     * @param param 参数集合
     * @return Integer Id集合
     * @author feng.zheng
     * @date 2019/2/21 15:18
     */
    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return inventoryProductDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public Integer deleteBatch(List<Long> idList, String corpid, Integer del) {
        if (CollectionsUtil.isEmpty(idList)) {
            return 0;
        }
        return inventoryProductDao.deleteBatch(idList, corpid, del);
    }

    @Override
    public List<BusinessProductEntity> getInventorySurplusNum(String corpid, Long inventoryId, boolean isInstock) {
        return inventoryProductDao.getInventorySurplusNum(corpid,inventoryId,isInstock);
    }

    @Override
    public Long isProductSubAttrExist(String attr, String corpid, Integer isArray) {
        return inventoryProductDao.isProductSubAttrExist(attr,corpid,isArray);
    }

    @Override
    public void insertBatch(List<InventoryProductEntity> addInventoryProductEntityList) {
        if (CollectionsUtil.isEmpty(addInventoryProductEntityList)) {
            return;
        }
        StandardizeDataUtil.standardize4InventoryProductEntityList(addInventoryProductEntityList);
        inventoryProductDao.insertBatch(addInventoryProductEntityList);
    }

    @Override
    public void updateBatch(List<InventoryProductEntity> updateBatchList, String corpid) {
        if (CollectionsUtil.isEmpty(updateBatchList)) {
            return;
        }
        long now = DateTimeUtil.getInt();
        for (InventoryProductEntity entity : updateBatchList) {
            entity.setUpdateTime(now);
        }
        StandardizeDataUtil.standardize4InventoryProductEntityList(updateBatchList);
        inventoryProductDao.updateBatch(updateBatchList,corpid);
    }

    @Override
    public List<BusinessProductEntity> findProductNum(Map<String, Object> param) {
        return inventoryProductDao.findProductNum(param);
    }

    @Override
    public Integer findProductNumCount(Map<String, Object> param) {
        return inventoryProductDao.findProductNumCount(param);
    }

    @Override
    public Double summaryByAttr(String field,String corpid,  Long refId, List<Long> productIdIn){
        return inventoryProductDao.summaryByAttr(field,corpid, refId, productIdIn);
    }

    @Override
    public Double summaryByAttrFromOutSide(String field,String corpid,  Long refId, List<Long> productIdIn){
        return inventoryProductDao.summaryByAttrFromOutSide(field,corpid, refId, productIdIn);
    }

    @Override
    public Long countProductSub(String attr, String corpid, Integer isArray) {
        return inventoryProductDao.countProductSub(attr, corpid, isArray);
    }

    @Override
    public List<InventoryProductEntity> getProductWithSubData(String attr, String corpid, Integer isArray, Long idGte, Integer limit) {
        return inventoryProductDao.getProductWithSubData(attr, corpid, isArray, idGte, limit);
    }


    @Override
    public Integer updateMulti(List<InventoryProductEntity> list, String corpid) {
        if (CollectionsUtil.isEmpty(list)) {
            return 0;
        }
        long now = DateTimeUtil.getInt();
        for (InventoryProductEntity entity : list) {
            entity.setUpdateTime(now);
        }
        StandardizeDataUtil.standardize4InventoryProductEntityList(list);
        return inventoryProductDao.updateMulti(list, corpid);
    }
}
